<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>到家的最少跳跃次数</title>
</head>
<body>
  <script>
    function fn(forbidden, a, b, x) {
      let cnt = 1000000;
      createTree(0, 0, false);
      return cnt < 1000000 ? cnt : -1;
      function createTree(val, count, isBackward) {
        if (val == x) {
          cnt = Math.min(cnt, count);
          return;
        }
        let forward = val + a;
        let backward = val - b;
        if (forward < 6000 && forbidden.indexOf(forward) == -1) {
          forbidden.push(forward); //防止循环
          createTree(forward, count + 1, false);
        }
        if (backward >= 0 && forbidden.indexOf(backward) == -1 && !isBackward) {
          createTree(backward, count + 1, true);
        }
      }
    }
    console.log(fn([14,4,18,1,15], 3, 15, 9))
  </script>
</body>
</html>